Imports System.Data
Imports System.Xml.Schema
Imports System.Xml


Partial Class Accounting_Dispatch
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call SetPageSecurity(divMain)
        Dim sec As New Secure

        With sec
            .ParentPage = Me
            .AddControl(Dispatch_btn, Secure.ControlTypes.AddType)
            .AddControl(btnPrint, Secure.ControlTypes.PrintType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 1, Secure.ControlTypes.EditType)
            .SetPageAccess(Session("UserName"))
        End With

        If Not Page.IsPostBack Then
            Call InitVars()
            Call PopulateDispatch()
            'Call Search()
        End If

    End Sub

    Private Sub PopulateDispatch()
        Dim dm As New DAOMain.CommonDAO

        Dim mSql As String = "Select distinct policy_dispatch_cd From vw_policy_dispatch_search Where policy_dispatch_cd <> ''"
        Dim dt As DataTable = dm.ExecQuery(mSql)

        With ddlDispatchPrint
            .DataSource = dt
            .DataTextField = "policy_dispatch_cd"
            .DataValueField = "policy_dispatch_cd"
            .DataBind()
            .Items.Insert(0, "")
        End With

        dm = Nothing
    End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call Search()
    End Sub

    Protected Sub Dispatch_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dispatch_btn.Click
        Dim dgi As DataGridItem

        Dim cf As New CIMSfunc
        Dim mID As String = cf.GetLastID("policy_dispatch_cd", "policy_dispatch_dtl")
        cf = Nothing

        For Each dgi In DataGrid.Items
            Dim ch As CheckBox = dgi.FindControl("Dispatch_ch")

            If Not IsNothing(ch) Then
                If ch.Checked Then
                    Dim vo As New DAOMain.PAYMENT_DTL_VO_DISPATCH
                    Dim dao As New DAOMain.PAYMENT_DTL_DAO_DISPATCH

                    vo.PAYMENT_SEQ_CD = dgi.Cells(4).Text
                    vo.PAYMENT_LAST_UPD_BY = Session("username")
                    vo.PAYMENT_LAST_UPD_DT = Now

                    Dim strAttach As String = ""
                    Dim txt As TextBox = dgi.FindControl("txtAttach")

                    If Not IsNothing(txt) Then
                        strAttach = txt.Text
                    End If

                    Call Me.UpdateBatchDispatch(dgi.Cells(4).Text, mID, strAttach)

                    dao.Update(vo)

                    vo = Nothing
                    dao = Nothing
                End If
            End If
        Next

        Dim pfunc As New PolyFunc.ASPFunc

        pfunc.MessageAlert(Me, "Selected Items Dispached")
        pfunc = Nothing

        Call Search()
        Call PopulateDispatch()
    End Sub

    Private Sub UpdateBatchDispatch(ByVal Payment_Seq_Cd As String, _
                                    ByVal Dispatch_Cd As String, _
                                    ByVal Attachment_desc As String)

        Dim dao As New DAOMain.POLICY_DISPATCH_DTL_DAO
        Dim vo As New DAOMain.POLICY_DISPATCH_DTL_VO

        Dim txnDate As Date = Format(Now, "M/d/yyyy")

        With vo
            .PAYMENT_SEQ_CD = Payment_Seq_Cd
            .POLICY_DISPATCH_CD = Dispatch_Cd
            .POLICY_DISPATCH_ATTACHMENT = Attachment_desc
            .POLICY_DISPATCH_DATE = txnDate
        End With

        dao.Create_Record(vo)

        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub DataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid.ItemDataBound
        Dim ch As CheckBox = e.Item.FindControl("Dispatch_ch")
        Dim txt As TextBox = e.Item.FindControl("txtAttach")

        If Not IsNothing(ch) Then
            Select Case Trim(e.Item.Cells(12).Text)
                Case "&nbsp;"
                    Dim lbtn As LinkButton = e.Item.FindControl("lbtnUpdate")
                    If Not IsNothing(lbtn) Then lbtn.Visible = False
                Case Else
                    ch.Enabled = False
                    'If Not IsNothing(txt) Then txt.ReadOnly = True
            End Select
        End If
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand

        Select Case e.CommandName
            Case "view_client"
                Session.Add("Seq_CD", e.Item.Cells(2).Text)
                Session.Add("pagesource", Request.FilePath)
                Session.Add("mode", "view")

                Response.Redirect("~/New_Business/ClientInfo.aspx")

            Case "Edit"
                Session.Add("Seq_CD", e.Item.Cells(2).Text)
                Session.Add("CID", e.Item.Cells(3).Text)
                Session.Add("CName", e.Item.Cells(4).Text)
                If Session("CName") = "&nbsp;" Then Session.Remove("CName")
                Session.Add("pagesource", "~/Accounting/Dispatch.aspx")

                Select Case Trim(e.Item.Cells(11).Text)
                    Case "&nbsp;"
                        Session.Add("mode", "edit")
                    Case Else
                        Session.Add("mode", "view")
                End Select

                Response.Redirect("~/Accounting/AddPayment.aspx")

            Case "update_attach"
                Dim txt As TextBox = e.Item.FindControl("txtAttach")

                If Not IsNothing(txt) Then
                    txt.Text = UCase(txt.Text)

                    Dim dm As New DAOMain.CommonDAO

                    With dm
                        .AddParameter(e.Item.Cells(4).Text)
                        .AddParameter(txt.Text)

                        .ExecCmdProc("sp_policy_dispatch_dtl_attach")
                    End With

                    dm = Nothing

                    Dim pf As New PolyFunc.ASPFunc
                    pf.MessageAlert(Me, "Attachment Updated!")

                    pf = Nothing
                End If
                
        End Select
        

        
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If e.NewPageIndex <= DataGrid.PageCount Then
            With DataGrid
                .CurrentPageIndex = e.NewPageIndex
                .DataSource = ViewState("dtable")
                .DataBind()
            End With
        End If
    End Sub

    Private Sub Search()
        Dim dao As New DAOMain.VW_POLICY_DISPATCH_SEARCH_DAO
        Dim vo As New DAOMain.VW_POLICY_DISPATCH_SEARCH_VO
        Dim cf As New CIMSfunc

        With vo
            If Trim(Startdate_txt.Text) <> "" Then .PAYMENT_DT = cf.ParseSetDate(Startdate_txt.Text)
            If Trim(Enddate_txt.Text) <> "" Then .PAYMENT_DT_END = cf.ParseSetDate(Enddate_txt.Text)
            .POLICY_CLIENT_LAST_NAME = "%" & Client_txt.Text & "%"
            .POLICY_CLIENT_FIRST_NAME = "%" & First_Name_txt.Text & "%"
            .PAYMENT_NO = "%" & AR_txt.Text & "%"
            .POLICY_NO = "%" & Policynumber_txt.Text & "%"
            .PRDT_DESC = "%" & Product_txt.Text & "%"
            .PROVIDER_DESC = "%" & Provider_txt.Text & "%"
        End With

        Dim dt As DataTable = dao.Search(vo, , True)
        ViewState.Add("dtable", dt)
        results_label.Text = dt.Rows.Count
        DataGrid.DataSource = dt
        DataGrid.CurrentPageIndex = 0
        DataGrid.DataBind()

        cf = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim dao As New DAOMain.VW_POLICY_DISPATCH_SEARCH_DAO
        Dim vo As New DAOMain.VW_POLICY_DISPATCH_SEARCH_VO

        vo.POLICY_DISPATCH_CD = Me.ddlDispatchPrint.SelectedValue
        Dim dt As DataTable = dao.Search(vo)

        Dim cr As New CReports

        cr.ShowRpt("Dispatch", CReports.RptType.PDF_Report, Me, dt, "vw_policy_dispatch_search")

        cr = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

#Region "old printing"
    'Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    '    Dim dgi As DataGridItem
    '    Dim i As Short
    '    Dim dao As New DAOMain.VW_POLICY_DISPATCH_SEARCH_DAO
    '    Dim vo As New DAOMain.VW_POLICY_DISPATCH_SEARCH_VO

    '    vo.POLICY_SEQ_CD = ""
    '    Dim dt As DataTable = dao.Search(vo)

    '    For Each dgi In DataGrid.Items
    '        Dim ch As CheckBox = dgi.FindControl("Print_ch")

    '        If Not IsNothing(ch) Then
    '            If ch.Checked Then
    '                Dim r As DataRow = dt.Rows.Add

    '                For i = 0 To 9
    '                    r.Item(i) = IIf((dgi.Cells(i + 2).Text = "&nbsp;"), "", dgi.Cells(i + 2).Text)
    '                Next i
    '            End If
    '        End If
    '    Next

    '    Dim cr As New CReports

    '    cr.ShowRpt("Dispatch", CReports.RptType.PDF_Report, Me, dt, "vw_policy_dispatch_search")

    '    cr = Nothing
    '    dao = Nothing
    '    vo = Nothing
    'End Sub
#End Region
    

    
    
End Class
